// pages/Protocol/RewardBill/RewardBill.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryRewardRankings } from '../../../api/protocol';
import { throttle } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    page: 0,
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
    dataList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
      dataList: [],
    })
    this.getData();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    console.log('上拉加载')
    if(this.data.isReachBottom){
      this._getGoods()
    }
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId')
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取数据
  getData(){
    let _this = this;
    var userId = wx.getStorageSync('userId');
    console.log(userId,'userId')
    queryRewardRankings({userId: userId,page: _this.data.page,isRank: '1'})
    .then(res => {
      if(res.success){
        // var list = _this.data.dataList;
        // if(_this.data.page == 0){
        //   list = res.data;
        // }else{
        //   list = list.concat(res.data);
        // }
        _this.setData({
          dataList: res.data,
          // total: res.data.totalElements,
          // page: _this.data.page+1,
        })
        console.log(_this.data.dataList,'dataList')
        // if(_this.data.page * 10 >= res.data.totalElements){
        //   _this.setData({
        //     isReachBottom: false,
        //     noMore: true,
        //     loading: false,
        //     loadingFailed: false,
        //   })
        // }else{
        //   _this.setData({
        //     isReachBottom: true,
        //     noMore: false,
        //     loading: false,
        //     loadingFailed: false,
        //   })
        // }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})